/**
 Reads gw_clone_names.txt. This file has Broad clones. Broad clones come in 2 words separated by a single space.
 We read the 1st word and print it alphabetically to see naming patterns.
*/

String filePath = "c:/search_results.csv"

String firstWord, secondWord, finalWord

def tokens

List words = new ArrayList(100)

List problemWords = new ArrayList(100)

int totalClones = 0, value = 0, ncIgnored = 0

new File(filePath).eachLine(){ line, lineNumber ->
      
      totalClones++
      
      // ignore the first line: header
      if (lineNumber == 1) {
         
         totalClones--
         return;
         
      }
      
      tokens = line.split("\\s")
      
      // first word
      firstWord = tokens[1] 
      
      // if the first word starts with NC ignore it
      if (firstWord[0..1] == "NC") {
       
         totalClones--
         ncIgnored++
         return;
         
      }
      
      else if (firstWord[0] == "X"){
         
         value = Integer.parseInt(firstWord[1..-1])
         
         if(value >= 1 && value <= 25) value += 106
         
         firstWord = "X${value}"
         
      }
      
      else if (firstWord[0] == "G"){
         
         value = Integer.parseInt(firstWord[1..-1])
         
         if(value >= 1 && value <= 25) value += 131
         
         firstWord = "X${value}"
         
      }
      
      // second word
      secondWord = tokens[2] 
      
      if (secondWord.length() == 2){
      
          if( (secondWord[0] as char).isDigit() ) secondWord = "0${secondWord[0..1]}"
          
          else if( (secondWord[1] as char).isDigit() ) secondWord = "${secondWord[0]}0${secondWord[1]}"
      
      }
      
      
      // check the final word
      finalWord = firstWord + secondWord
      if(finalWord.length() != 7) {
      
         problemWords.add(finalWord)
         
      } else {
      
         words.add(finalWord)
         
      }

      
}

// print the result
println "Total clones transformed: ${totalClones}"
println "Total ignored for NC: ${ncIgnored}"
println "Processed words: ${words} Size: ${words.size()}"
println "Problem words: ${problemWords} Size: ${problemWords.size()}"